/*
 *
 * *******************************************************************
 *   @项目名称: BHex Android
 *   @文件名称: SecurityApi.java
 *   @Date: 11/29/18 3:21 PM
 *   @Author: chenjun
 *   @Copyright（C）: 2018 BlueHelix Inc.   All rights reserved.
 *   注意：本内容仅限于内部传阅，禁止外泄以及用于其他的商业目的.
 *  *******************************************************************
 *
 */

package io.bhex.sdk.security;

import io.bhex.sdk.Urls;
import io.bhex.sdk.security.bean.GAInfoResponse;
import io.bhex.sdk.security.bean.OrderIdParamResponse;
import io.bhex.baselib.network.BParamsBuilder;
import io.bhex.baselib.network.HttpUtils;
import io.bhex.baselib.network.listener.SimpleResponseListener;
import io.bhex.baselib.network.params.PostParams;
import io.bhex.baselib.network.response.ResultResponse;
import io.bhex.baselib.utils.MD5Utils;

/**
 * ================================================
 * 描   述：安全Api 包括绑定和解绑GA、邮箱、手机
 * ================================================
 */

public class SecurityApi {

    /**
     * 获取绑定GA所需要的信息
     *
     * @param listener
     */
    public static void getBindGAInfo(SimpleResponseListener<GAInfoResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_BIND_GA_INFO)
                .build();
        HttpUtils.getInstance().request(params, GAInfoResponse.class, listener);
    }

    /**
     * 请求绑定邮箱-发送邮箱验证码 第一步
     *  @param email
     * @param token
     * @param deepknowId
     * @param listener
     */
    public static void requestEmailVerifyCodeOfBindEmail(String email, String token, String deepknowId, SimpleResponseListener<OrderIdParamResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_EMAIL_URL)
                .addParam("type", "6")
                .addParam("email", email)
                .addParam("captcha_response", token)
                .addParam("captcha_id", deepknowId)
                .build();

        HttpUtils.getInstance().request(params, OrderIdParamResponse.class, listener);
    }

    /**
     * 请求绑定手机-发送手机验证码 第一步
     *
     * @param mobile
     * @param token
     * @param listener
     */
    public static void requestMobileVerifyCodeOfBindMobile(String mobile, String token,String deepknowId, String nationalCode, SimpleResponseListener<OrderIdParamResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_GET_BASIC_VERIFY_CODE_FIRST_MOBILE_URL)
                .addParam("type", "5")
                .addParam("national_code", nationalCode)
                .addParam("mobile", mobile)
                .addParam("captcha_response", token)
                .addParam("captcha_id", deepknowId)
                .build();
        HttpUtils.getInstance().request(params, OrderIdParamResponse.class, listener);
    }

    /**
     * 请求绑定手机-发送邮箱验证码 第二步
     *
     * @param listener
     */
    public static void requestEmailVerifyCodeOfBindMobile(SimpleResponseListener<OrderIdParamResponse> listener) {
//        PostParams params = BParamsBuilder.post()
//                .url(Urls.API_GET_VERIFY_CODE_EMAIL_URL)
//                .addParam("type","5")
//                .build();
//        HttpUtils.getInstance().request(params,OrderIdParamResponse.class,listener);
        sendVerifyCodeWithToken(true, "5", listener);
    }

    /**
     * 请求绑定邮箱-发送手机验证码 第二步
     *
     * @param listener
     */
    public static void requestMobileVerifyCodeOfBindEmail(SimpleResponseListener<OrderIdParamResponse> listener) {
//        PostParams params = BParamsBuilder.post()
//                .url(Urls.API_GET_VERIFY_CODE_MOBILE_URL)
//                .addParam("type","6")
//                .build();
//        HttpUtils.getInstance().request(params,OrderIdParamResponse.class,listener);
        sendVerifyCodeWithToken(false, "6", listener);
    }

    /**
     * 绑定邮箱
     *
     * @param email
     * @param orderIdOfEmail
     * @param emailCode
     * @param orderIdOfMobile
     * @param mobileCode
     * @param listener
     */
    public static void bindEmail(String email, String orderIdOfEmail, String emailCode, String orderIdOfMobile, String mobileCode, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_BIND_EMAIL)
                .addParam("email", email)
                .addParam("email_order_id", orderIdOfEmail)
                .addParam("email_verify_code", emailCode)
                .addParam("order_id", orderIdOfMobile)
                .addParam("verify_code", mobileCode)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * 绑定手机
     *
     * @param nationalCode    手机区号
     * @param mobile
     * @param orderIdOfMobile
     * @param mobileCode
     * @param orderIdOfEmail
     * @param emailCode
     * @param listener
     */
    public static void bindMobile(String nationalCode, String mobile, String orderIdOfMobile, String mobileCode, String orderIdOfEmail, String emailCode, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_BIND_MOBILE)
                .addParam("national_code", nationalCode)
                .addParam("mobile", mobile)
                .addParam("mobile_order_id", orderIdOfMobile)
                .addParam("mobile_verify_code", mobileCode)
                .addParam("order_id", orderIdOfEmail)
                .addParam("verify_code", emailCode)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * 发送验证码（已登录状态下）
     *
     * @param isEmail
     * @param type
     * @param listener
     */
    private static void sendVerifyCodeWithToken(boolean isEmail, String type, SimpleResponseListener<OrderIdParamResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(isEmail ? Urls.API_GET_VERIFY_CODE_EMAIL_URL : Urls.API_GET_VERIFY_CODE_MOBILE_URL)
                .addParam("type", type)
                .build();
        HttpUtils.getInstance().request(params, OrderIdParamResponse.class, listener);
    }

    /**
     * 设置或者修改资金密码
     * @param isEmail 注册账号是邮箱账号还是手机账号 true 邮箱 false 手机
     * @param isSetFundPwd true设置资金密码 false 修改资金密码
     * @param listener
     */
    public static void sendFundVerifyCode(boolean isEmail, boolean isSetFundPwd, SimpleResponseListener<OrderIdParamResponse> listener) {
        sendVerifyCodeWithToken(isEmail, isSetFundPwd ? "15" : "16", listener);
    }

    /**
     * 发送绑定Ga的验证码
     * @param isEmail 注册账号是邮箱账号还是手机账号 true 邮箱 false 手机
     * @param listener
     */
    public static void sendBindGAVerifyCode(boolean isEmail, SimpleResponseListener<OrderIdParamResponse> listener) {
        sendVerifyCodeWithToken(isEmail, "7", listener);
    }

    /**
     * 设置资金密码
     * @param isSetFinancePasswd
     * @param passwd
     * @param confirmPasswd
     * @param orderId
     * @param verifyCode
     * @param listener
     */
    public static void setFinancePasswd(boolean isSetFinancePasswd,String passwd,String confirmPasswd,String orderId,String verifyCode, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_SET_TRADE_PASSWD)
                .addParam("type", isSetFinancePasswd?"1":"2")
                .addParam("trade_pwd", MD5Utils.encode(passwd))
                .addParam("trade_pwd2", MD5Utils.encode(confirmPasswd))
                .addParam("order_id", orderId)
                .addParam("verify_code", verifyCode)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    public static void bindGA(String orderId,String verifyCode, String googleVerifyCode, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_BIND_GA)
                .addParam("ga_code",googleVerifyCode)
                .addParam("order_id", orderId)
                .addParam("verify_code", verifyCode)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * KYC认证
     * @param country
     * @param firstName
     * @param lastName
     * @param idType
     * @param id
     * @param gender
     * @param photoUrl
     * @param photoUrl2
     * @param twoVerifyType
     * @param orderId
     * @param verifyCode
     * @param listener
     */
    public static void authKYC(String country, String firstName, String lastName, String idType, String id, String gender, String photoUrl, String photoUrl2,int twoVerifyType,String orderId,String verifyCode, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_AUTHENTICATE)
                .addParam("nationality", country)
                .addParam("first_name", firstName)
                .addParam("second_name", lastName)
                .addParam("gender", gender)
                .addParam("card_type", idType)
                .addParam("card_no", id)
                .addParam("card_front_url", photoUrl)
                .addParam("card_hand_url", photoUrl2)
                .addParam("auth_type", twoVerifyType)
                .addParam("order_id", orderId)
                .addParam("verify_code", verifyCode)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }

    /**
     * KYC认证
     * @param country
     * @param firstName
     * @param lastName
     * @param idType
     * @param id
     * @param gender
     * @param photoUrl
     * @param photoUrl2
     * @param listener
     */
    public static void authKYCNew(String country, String firstName, String lastName, String idType, String id, String gender, String photoUrl, String photoUrl2, SimpleResponseListener<ResultResponse> listener) {
        PostParams params = BParamsBuilder.post()
                .url(Urls.API_AUTHENTICATE)
                .addParam("nationality", country)
                .addParam("first_name", firstName)
                .addParam("second_name", lastName)
                .addParam("gender", gender)
                .addParam("card_type", idType)
                .addParam("card_no", id)
                .addParam("card_front_url", photoUrl)
                .addParam("card_hand_url", photoUrl2)
                .build();
        HttpUtils.getInstance().request(params, ResultResponse.class, listener);
    }
}
